/*
 * @(#)GSOrgProcessor.java
 * 
 * Project: BUS
 * 
 * Modify Information:
 * =============================================================================
 * Author           Date            Description 
 * ------------     ----------     -----------------------------
   Simonyi Peng     2006-2-7        建立
 * 
 * Copyright Notice:
 * =============================================================================
 * Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room,
 * Science & Technology Building, No. 9 South Street, Zhong Guan Cun, Haidian
 * District, Beijing ,100081, China All rights reserved.
 * 
 * This software is the confidential and proprietary information of Beijing
 * HiTRUST Technology Co., Ltd. ("Confidential Information"). You shall not
 * disclose such Confidential Information and shall use it only in accordance
 * with the terms of the license agreement you entered into with HiTRUST.
 * 
 * Warning:
 * =============================================================================
 *  
 */
package com.hitrust.gs.business;

import com.hitrust.gs.bean.*;
import com.hitrust.gs.service.*;
import com.hitrust.gs.util.exception.*;
import com.hitrust.gs.dao.*;
import java.util.*;
import com.hitrust.gs.lib.GSLogWriter;

import java.sql.Connection;


public class GSOrgProcessor implements GSBaseProcesser {

    public GSOrganizationBean getOrgByCode(String orgCode) throws GSException {
        GSOrganizationDAO tGSOrgDAO = new GSOrganizationDAO();
        return tGSOrgDAO.getOrg(orgCode);
    }

    public String getParentOrg() {
        return null;
    }
    
    /**
     * 取得操作员所属机构及该机构的下级机构
     * @param aOperOrgId        操作员所在机构
     * @return                  
     * @throws GSException
     */
    public ArrayList getOperOrgAndSubOrg(String aOperOrgId) throws GSException{
        ArrayList tList = new ArrayList();
        Connection tConn = null;
        GSOrganizationDAO tGSOrgDAO = new GSOrganizationDAO();
        try{
            
            tConn = tGSOrgDAO.getConnetion();
            //查询操作员所在机构
            GSOrganizationBean tGSOrg = tGSOrgDAO.getOrg(tConn,aOperOrgId);
            tList.add(tGSOrg);
            //操作员所在机构所有的下级机构
            ArrayList tList2 = tGSOrgDAO.getSubOrg(tConn,aOperOrgId);
            //getSubOrg方法在aOperOrgId为巴士物资部门的时候将得到所有机构，需要将重复的自己删除掉
            for(int i=0; i<tList2.size(); i++){
                tGSOrg = (GSOrganizationBean)tList2.get(i);
                //如果当前机构id等于aOperOrgId，则为自己，不重复加入tList中
                if(!aOperOrgId.equals(tGSOrg.getGs_org_id()))
                    tList.add(tGSOrg);
            }
        }catch(GSException ge){
            throw ge;
        }catch(Exception e){
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E20031,GSException.E20031_MSG);
        }finally{
            GSOrganizationDAO.close(tConn);
        }
        return tList;
    }

    /**
     * 得到组织机构以及它的下属机构
     * 并根据订单输入页面的要求，构造成格式化字符串的形式返回
     * @param orgId
     * @return
     * @throws GSException
     */
    public ArrayList getSubOrgAndInfo(String orgId) throws GSException {
        GSOrganizationDAO dao = new GSOrganizationDAO();
        ArrayList returnValue = new ArrayList(2);

        //得到下属机构列表
        ArrayList ary = this.getOperOrgAndSubOrg(orgId);
        //从下属机构列表构造页面需要的字符串列表
        ArrayList tList = new ArrayList();
        ArrayList temp = new ArrayList();
        for(int i=0;i<ary.size();i++){
            GSOrganizationBean bean = (GSOrganizationBean)ary.get(i);
            //过滤不是订货单位的机构
            if("0".equals(bean.getGs_is_stock()))
            	continue;
            String str = bean.getGs_org_id()+"|"+bean.getGs_org_name();
            tList.add(str);
            
            ArrayList deptInfoList = dao.getDeptInfoList(bean.getGs_org_id()) ;
            //如果没有常用联系单位信息，置空串
            if(deptInfoList.size()==0) deptInfoList.add("");
            temp.add(deptInfoList);
        }
        //缓冲
        returnValue.add(0,tList);
        returnValue.add(1,temp);
        return returnValue;
    }
    
    /**
     * 判断机构级别,0代表无法判断,1代表巴士物资,2代表巴士物资部门,3代表运营公司,4代表修理厂或者运营公司部门
     * @param ordId
     * @return
     */
    public static int judgeOrgLevel(String orgId){
        if(orgId ==null || orgId.length() != 4)
            return 0;
        String a1 = orgId.substring(0,2);
        String a2 = orgId.substring(2);
        if(!"00".equals(a1) && "00".equals(a2))
            return 3;
        else if("00".equals(a1) && !"00".equals(a2))
            return 2;
        else if("0000".equals(orgId))
            return 1;
        else return 4;
    }
    
}